﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using DAO;
using BUS;

namespace GUI
{
    public partial class PhieuThuTien : UserControl
    {
        public PhieuThuTien()
        {
            InitializeComponent();
        }

        private PhieuThuDTO _phieuThu= new PhieuThuDTO();
        private DocGiaDTO _docGia= new DocGiaDTO();
        public delegate void GetString(String str);
        public GetString MyGetData;
        //Ham phu tro
        private void LoadData()
        {
            dGVPhieuThu.DataSource =PhieuThuBUS.HienThiBang();
            cbSothe.DataSource = DocGiaBUS.TimTatCaDocGia();
            cbSothe.DisplayMember = "Số Thẻ";
            cbSothe.ValueMember = "Số thẻ";
          _docGia= DocGiaBUS.TimDocGia(cbSothe.Text);
            tbTenDocGia.Text = _docGia.HoTen;
            HienThiSoTienNo();
            

        }

       private bool CheckDaDien()
       {
           if (tbMaPhieuThu.Text==""||tbSoTienThu.Text==""||tbTenDocGia.Text==""||cbSothe.Text==""||dTimeNgayThu.Text=="")
           {
               return false;
           }
         
           return true;
       }
        private bool CheckTienThu()
        {
            if (decimal.Parse(tbSoTienThu.Text) > decimal.Parse(tbSoTienNo.Text))
            {
                tbThongBao.Text = "Không được thu quá số tiền nợ";
                return false;
            }
            return true;
        }
        private bool CheckUpdate(DocGiaDTO docgia)
        {
            if (docgia.TongTienNo < 0)
            {
                return false;
            }
            return true;   
        }

        private void UpLoadData()//tao Phieu Thu DTO
        {
            _phieuThu.SoThe = cbSothe.Text;
            _phieuThu.MaPhieuThu = tbMaPhieuThu.Text;
            _phieuThu.NgayThu = DateTime.Parse(dTimeNgayThu.Text);
            _phieuThu.SoTienThu = decimal.Parse(tbSoTienThu.Text);
        }
        private void tbThongBao_TextChanged(object sender, EventArgs e)
        {
            if (MyGetData!=null)
            {
                MyGetData(tbThongBao.Text);
            }
        }

        //Ham thiet ke
        private void PhieuThuTien_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void dGVPhieuThu_SelectionChanged(object sender, EventArgs e)
        {
            if (dGVPhieuThu.CurrentRow!=null)
            {
                try
                {
                    tbMaPhieuThu.Text = dGVPhieuThu.SelectedRows[0].Cells[0].Value.ToString();
                    cbSothe.Text = dGVPhieuThu.SelectedRows[0].Cells[1].Value.ToString();
                    tbTenDocGia.Text = dGVPhieuThu.SelectedRows[0].Cells[2].Value.ToString();
                    tbSoTienThu.Text = dGVPhieuThu.SelectedRows[0].Cells[3].Value.ToString();
                    dTimeNgayThu.Text = dGVPhieuThu.SelectedRows[0].Cells[4].Value.ToString();
                }
                catch (Exception)
                {                    
                    
                }
            }
        }

        private void cbSothe_SelectedIndexChanged(object sender, EventArgs e)
        {
            _docGia = DocGiaBUS.TimDocGia(cbSothe.Text);
            tbTenDocGia.Text = _docGia.HoTen;
            try
            {
                tbMaPhieuThu.Text = TaoMaNgauNhien();
            }
            catch (Exception)
            {                                
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (CheckDaDien()&&CheckTienThu())
            {
                if(PhieuThuDAO.CheckPhieuThuTienByID(tbMaPhieuThu.Text))
                {
                    tbThongBao.Text = "Mã phiếu thu đã tồn tại.";  
                }
                else
                {
                    _docGia = DocGiaDAO.TimDocGia(cbSothe.Text);
                    DocGiaDTO _docGia1 = _docGia;
                    UpLoadData();
                    if (PhieuThuDAO.CheckPhieuThuTienByID(tbMaPhieuThu.Text))
                    {
                        tbThongBao.Text = "Mã phiếu thu đã tồn tại";
                    }
                    else
                    {
                      
                        if (PhieuThuBUS.ThemPhieuThu(_phieuThu))
                        {                            
                            LoadData();                           
                            _docGia1.TongTienNo -= _phieuThu.SoTienThu;
                            DocGiaDAO.SuaDocGia(_docGia1);
                            tbThongBao.Text = "Thêm phiếu thu thành công.";
                        }
                    }
                }
            }
                           
            else
            {
                tbThongBao.Text = "Bạn chưa điền đầy đủ thông tin hoặc số tiền thu quá mức số tiền nợ";
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (CheckDaDien())
            {
                _docGia = DocGiaDAO.TimDocGia(cbSothe.Text);
                DocGiaDTO _docGia1 = _docGia;
                _phieuThu = PhieuThuDAO.TimPhieuThuTien(tbMaPhieuThu.Text);
                _docGia1.TongTienNo += _phieuThu.SoTienThu;
                decimal n = _phieuThu.SoTienThu;
                decimal m = _docGia1.TongTienNo + _phieuThu.SoTienThu - decimal.Parse(tbSoTienThu.Text);
                UpLoadData();
                if (m<0)
                {
                    tbThongBao.Text = "Bạn đã điền quá số tiền trả, yêu cầu điền lại.";
                    return;
                }
                if (PhieuThuBUS.SuaPhieuThu(_phieuThu))
                {
                    LoadData();
                    //_docGia1 = DocGiaDAO.TimDocGia(cbSothe.Text);
                    _docGia1.TongTienNo -= _phieuThu.SoTienThu;
                    DocGiaDAO.SuaDocGia(_docGia1);
                    tbThongBao.Text = "Bạn đã sửa phiếu thu thành công.";
                }
                else
                {
                    tbThongBao.Text = "Sửa thất bại.";
                    _docGia1.TongTienNo -= n;
                    DocGiaDAO.SuaDocGia(_docGia1);
                }
            }
            else
            {
                tbThongBao.Text = "Bạn chưa điền đầy đủ thông tin hoặc số tiền thu quá mức số tiền nợ";
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (PhieuThuDAO.CheckPhieuThuTienByID(tbMaPhieuThu.Text))
            {
                _phieuThu = PhieuThuDAO.TimPhieuThuTien(tbMaPhieuThu.Text);
                _docGia = DocGiaDAO.TimDocGia(cbSothe.Text);
                DocGiaDTO _docgia1 = _docGia;
                _docgia1.TongTienNo += _phieuThu.SoTienThu;
                DocGiaDAO.SuaDocGia(_docgia1);
                if (PhieuThuBUS.XoaPhieuThu(tbMaPhieuThu.Text))
                {
                                      
                    LoadData();
                    tbThongBao.Text = "Xóa thành công.";
                }
                else
                {
                    tbThongBao.Text = "Xóa thất bại";
                    _docgia1.TongTienNo -= _phieuThu.SoTienThu;
                    DocGiaDAO.SuaDocGia(_docgia1);
                }
            }
            else
            {
                tbThongBao.Text = "Không tìm thấy mã phiếu thu muốn xóa.";
            }
           
        }

        private void btnReFresh_Click(object sender, EventArgs e)
        {
            LoadData();
            tbMaPhieuThu.Text = "";
            tbSoTienNo.Text = "";
            tbSoTienThu.Text = "";
            tbTenDocGia.Text = "";
            cbSothe.Text = "";
            dTimeNgayThu.Text = "";
        }

        private void tbSoTienThu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+") == true || e.KeyChar == '\b')
                e.Handled = false;
            else
            {
                e.Handled = true;
            }
        }

        private void cbSothe_TextChanged(object sender, EventArgs e)
        {
               HienThiSoTienNo();
        }
        private void HienThiSoTienNo()
        {
            _docGia = DocGiaDAO.TimDocGia(cbSothe.Text);
            tbSoTienNo.Text = (_docGia.TongTienNo.ToString());
        }
        private String TaoMaNgauNhien()
        {
            string returnid = "";
            Random rd = new Random();
            do
            {
                returnid = "PTT" + DateTime.Now.Day.ToString().PadLeft(2, '0') + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Year.ToString().Substring(2, 2) + rd.Next(1, 999).ToString().PadLeft(3, '0');
                if (returnid.Length > 12)
                {
                    return "";
                }
            }
            while (PhieuThuDAO.CheckPhieuThuTienByID(returnid));
            return returnid;
        }
    }
}
